package com.kurumi.medical.util;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Properties;

public class DBUtil {

	public static void close(Connection conn, PreparedStatement pst, ResultSet rs) {
	   try {
		   if (rs != null) {
			   rs.close();
		   }
		   
		   if (pst != null) {
		    	  pst.close();
		      }
		   if (conn != null) {
		    	  conn.close();
		      }
		      
	        } catch (SQLException e) {
	            e.printStackTrace();
	        }
    }

	
	// 对外提供一个方法来获取数据库连接
	public static Connection getConnection() {
		Connection conn = null;
		try {
			Properties pro = PropsUtil.loadProps("application.properties");
			String url = pro.getProperty("sqlServer.url");
			System.out.println("user------" + url);
			String user = pro.getProperty("sqlServer.user");
			String password = pro.getProperty("sqlServer.password");
			System.out.println(url + "--" + user +"-------" + password);
			// 1.加载驱动程序
			Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
			//DriverManager.registerDriver(new com.microsoft.sqlserver.jdbc.SQLServerDriver());
			// 2.获得数据库的连接
			 conn = (Connection) DriverManager.getConnection(url, user, password);
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return conn;
	}
	
	
}


